package com.segersten.othello.service.akka.lobby.dao;

import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;

import com.beust.jcommander.internal.Maps;
import com.segersten.othello.service.akka.lobby.BoardReference;
import com.segersten.othello.service.akka.lobby.DomainPlayer;
import com.segersten.othello.service.akka.lobby.PlayerScore;

public class InMemoryPlayerScoreDao implements PlayerScoreDao
{
    private static final PlayerScore NULL_SCORE = new PlayerScore();

    private Map<DomainPlayer, PlayerScore> scores = Maps.newHashMap();
    
    Logger logger = Logger.getLogger(InMemoryPlayerScoreDao.class.getSimpleName());

    @Override
    public void updateScores(BoardReference boardReference, Map<DomainPlayer, Integer> playerScores)
    {
        for (Entry<DomainPlayer, Integer> entry : playerScores.entrySet()) {
            Integer score = playerScores.get(entry.getKey());
            addPoints(entry.getKey(), score);
        }
    }

    private void addPoints(DomainPlayer key, Integer score)
    {
        if (scores.containsKey(key)==false) {
            scores.put(key, new PlayerScore());
        }
        scores.get(key).increase(score);
    }

    @Override
    public PlayerScore getPoints(DomainPlayer player)
    {
        return scores.containsKey(player) ? scores.get(player) : NULL_SCORE;
    }

}
